import 'package:enterprise_v6_app/app/components/custom_refresh.dart';
import 'package:enterprise_v6_app/app/core/constans/dict_data_info.dart';
import 'package:enterprise_v6_app/app/core/http/http.dart';
import 'package:enterprise_v6_app/app/core/utils/interface.dart';
import 'package:enterprise_v6_app/app/data/entities/application/filter/filter_bean.dart';
import 'package:enterprise_v6_app/app/data/entities/application/filter/filter_entities_bean.dart';
import 'package:enterprise_v6_app/app/data/entities/application/standing_book/operating_ledger_entity.dart';
import 'package:enterprise_v6_app/router/app_pages.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';

class OperatingLedgerController extends GetxController {
  final GlobalKey<CustomEasyRefreshListState> globalKey = GlobalKey();
  /// 筛选右侧栏控制Key
  final popRightScaffoldKey = GlobalKey<ScaffoldState>();

  // 搜索字符串
  String? search;

  // 开始时间
  String startTime = '';

  // 结束时间
  String endTime = '';

  // 异常列表 0：管理异常 1：开票异常 2：气体异常 3：其他异常
  final List<String> operatingLedgerList = [
    '管理异常',
    '气体异常',
    '开票异常',
    '其它异常',
  ];

  // 选中的异常列表
    List<FilterEntitiesBean> selectedOperatingLedgerList = [];

  // 筛选列表
  List<FilterBean> filterList = [];

  // 选中的作业类型
  List<FilterEntitiesBean> selectType = [];

  // 请求参数
  Map<String, dynamic> queryParameters = {};

  // 测试筛选值
  init() {
    final List<FilterEntitiesBean> listType = [];

    for (var value1 in DictDataInfo.specialJobType) {
      listType.add(FilterEntitiesBean(name: value1.dictionaryLabel.toString(), value: value1.id));
    }

    final List<FilterEntitiesBean>  list =[];
    for(var i in operatingLedgerList) {
      list.add(FilterEntitiesBean(name: i,value: operatingLedgerList.indexOf(i)));
    }

    //筛选控制bean
    final FilterBean filterBean =
        FilterBean(title: '作业类型', filterList: listType);
    final FilterBean filterBean3 = FilterBean(title: '异常状态', filterList: list);
    filterList.add(filterBean);
    filterList.add(filterBean3);
  }

  // 点击筛选按钮
  void screenClick() {
    popRightScaffoldKey.currentState?.openEndDrawer();
    // CustomFilterBottomSheet(
    //   dataList: filterList,
    //   onConfirm: (List<FilterBean> value) {
    //     filterList = value;
    //     for(var i in value) {
    //       if(i.title == '作业类型') {
    //         selectType = i.chosenValue;
    //       }
    //     }
    //     refreshPage();
    //   },
    // ).showBottomSheetDialog();
  }

  // 筛选确认
  void screenConfirm(List<FilterBean> value) {
    filterList = value;
    for (var i in value) {
      if (i.title == '作业类型') {
        selectType = i.chosenValue;
      }
      if (i.title == '异常状态') {
        selectedOperatingLedgerList = i.chosenValue;
      }
    }
    update();
    refreshPage();
  }

  // 异常点击事件
  void operatingLedgerClick(String operatingLedger) {
    // if (selectedOperatingLedgerList.contains(operatingLedger)) {
    //   selectedOperatingLedgerList.remove(operatingLedger);
    // } else {
    //   selectedOperatingLedgerList.add(operatingLedger);
    // }
    // refreshPage();
    // update();
  }

  // 查看详情
  void operatingLedgerDetailsClick(
      OperatingLedgerReceiptLists operatingLedgerEntity) {
    Get.toNamed(AppRoutes.jobLedgerDetail, arguments: operatingLedgerEntity);
  }

  // 获取作业台账列表数据
  Future<List<OperatingLedgerEntity>> getOperatingLedgerList(
      int pageNum, int pageSize,
      {String? query}) async {
    queryParameters['pageNum'] = pageNum;
    queryParameters['pageSize'] = pageSize;
    queryParameters['keywords'] = search;
    queryParameters['startDate'] = startTime;
    queryParameters['endDate'] = endTime;
    final List<String> index = [];
    for (var i in selectedOperatingLedgerList) {
      index.add(i.value.toString());
    }
    queryParameters['abnormalTypes'] = index.join(',');

    final List<String> index2 = [];
    for (var i in selectType) {
      index2.add(i.value.toString());
    }
    queryParameters['ticketBigTypeIds'] = index2.join(',');
    List<OperatingLedgerEntity> data = [];
    final res = await HttpUtil()
        .generateGetRequest<List<OperatingLedgerEntity>>(
          Interface.getOperatingLedgerList,
          queryParameters: queryParameters,
        )
        .execute();
    res.onSuccess((result) {
      if (result.data != null) {
        data = result.data!;
      }
    });
    return data;
  }

  // 搜索
  void refreshPage() {
    globalKey.currentState?.onRefresh();
  }

  @override
  void onReady() {
    super.onReady();
    init();
  }
}
